﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Catsland.Core
{
	public class ModelList : UniqueList<CatsModel>
	{
		public void AddModel(CatsModel model)
		{
			base.AddItem(model._name, model);

			if (Mgr<GameEngine>.Singleton._gameEngineMode
				== GameEngine.GameEngineMode.MapEditor)
			{
				Mgr<GameEngine>.Singleton.Editor.UpdateModelList(m_list);
			}

		}

		public override void ReleaseAll()
		{
			base.ReleaseAll();

			if (Mgr<GameEngine>.Singleton._gameEngineMode
				== GameEngine.GameEngineMode.MapEditor)
			{
				Mgr<GameEngine>.Singleton.Editor.UpdateModelList(m_list);
			}
		}

		public CatsModel GetModel(String name)
		{
			return base.GetItem(name);
		}

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement models = doc.CreateElement("Models");
			node.AppendChild(models);

			foreach (KeyValuePair<String, CatsModel> pair in m_list)
			{
				pair.Value.SaveToNode(models, doc);
			}

			return true;
		}

		public static ModelList LoadFromNode(XmlNode node, Scene scene)
		{
			ModelList newModelList = new ModelList();

			foreach (XmlNode model in node.ChildNodes)
			{
				CatsModel newModel = CatsModel.LoadFromNode(model, scene);
				newModelList.AddModel(newModel);
			}

			return newModelList;
		}
	}
}
